home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Modules / BackSpaceModules / Source / BattleView / BattleView.m < prev    next >
Text File  |  1994-05-04  |  5KB  |  229 lines

  1. /* BattleView.h -- implementation for main view
  2.    Copyright (C) 1992, 1993 David A. Strout
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David Strout <dstrout@isi.edu>. */
  19.  
  20.  
  21. #import "BattleView.h"
  22. #import "Ship.h"
  23. #import "Misc.h"
  24. #import "Weapon.h"
  25. #import "Thinker.h"
  26.  
  27. @implementation BattleView
  28.  
  29. - initFrame:(const NXRect *)frm
  30. {
  31.   int i;
  32.   id theSpaceThing;
  33.   
  34.   
  35.   INITRAND;
  36.   
  37. /* Nothing majic about 12, just a nice number to start */
  38.   theFedList=[[List alloc] initCount:12];
  39.   theKliList=[[List alloc] initCount:12];
  40.   theWeaponList=[[List alloc] initCount:12];
  41.   
  42.   for(i=0;i<NUMSHIPS;i++){
  43.     theSpaceThing=[[Ship alloc] initOnSide:FED];
  44.     [theFedList addObject:theSpaceThing];
  45.   };
  46.   
  47.   for(i=0;i<NUMSHIPS;i++){
  48.     theSpaceThing=[[Ship alloc] initOnSide:KLI];
  49.     [theKliList addObject:theSpaceThing];
  50.   };
  51.   
  52.   [self display];
  53.   
  54.   return self;
  55. }
  56.  
  57. - drawSelf:(NXRect *)rects :(int)rectCount 
  58. {
  59.   if (!rects || !rectCount) return self;
  60.   PSsetgray(0);
  61.   NXRectFill(rects);
  62.   return self;
  63. }
  64.  
  65. - (float)xSize
  66. {
  67.   return bounds.size.width;
  68. }
  69.  
  70. - (float)ySize
  71. {
  72.   return bounds.size.height;
  73. }
  74.  
  75.  
  76. - drawPhaser
  77. /* Walks through the WeaponList drawing everything.  Doesn't belong here, never got around to moving it. */
  78. {
  79.   int i;
  80.   NXPoint *from, *to;
  81.   
  82.  
  83.   for(i=0;i<[theWeaponList count];i++) {
  84.     
  85.     from=[[theWeaponList objectAt:i] from];
  86.     to=[[theWeaponList objectAt:i] to];
  87.     
  88.     PSsetrgbcolor((float)(RANDINT(10)*.1), (float)(RANDINT(10)*.1), (float)(RANDINT(10)*.1));
  89.  
  90.     PSmoveto(from->x+8, from->y+8);
  91.     PSlineto(to->x+8, to->y+8);
  92.   };
  93.   
  94.   PSstroke();
  95.   PSflushgraphics();
  96.  
  97.   return self;
  98. }
  99.  
  100. - erasePhaser
  101. /* Same as drawPhaser, only draws them in black */
  102. {
  103.   int i;
  104.   NXPoint *from, *to;
  105.   
  106.  
  107.   for(i=0;i<[theWeaponList count];i++) {
  108.     
  109.     from=[[theWeaponList objectAt:i] from];
  110.     to=[[theWeaponList objectAt:i] to];
  111.     
  112.     PSsetgray(0);
  113.     PSmoveto(from->x+8, from->y+8);
  114.     PSlineto(to->x+8, to->y+8);
  115.     [[theWeaponList removeObjectAt:i] free];
  116.   };
  117.   
  118.   PSstroke();
  119.   PSflushgraphics();
  120.  
  121.   return self;
  122. }
  123.  
  124. - shootPhaser
  125. /* Picks a ship at random (the "shooter") then finds the closest enemy. Then we create an instance of Weapon, add it to theWeaponList, and tell that ship to take some damage */
  126. {
  127.   int i,j, target, shooter;
  128.   double distance, minDist;
  129.   id theShot;
  130.   NXPoint from, to;
  131.  
  132.   /* Do the Federation */
  133.   minDist=10000.0;
  134.   for(i=0;i<SHOTS_PER_FRAME;i++) {
  135.     theShot=[[Weapon alloc] init];
  136.     shooter=RANDINT(NUMSHIPS-1);
  137.     from.x=[[theFedList objectAt:shooter] xLoc];
  138.     from.y=[[theFedList objectAt:shooter] yLoc];
  139.     
  140.     target=0; minDist=10000.0; 
  141.     for(j=0;j<NUMSHIPS;j++) {
  142.       to.x=[[theKliList objectAt:j] xLoc];
  143.       to.y=[[theKliList objectAt:j] yLoc];
  144.       
  145.       distance=sqrt(pow(fabs((double)to.x-(double)from.x), 2)+pow(fabs((double)to.y-(double)from.y), 2));
  146.       if( distance <= minDist ){
  147.     target=j;
  148.     minDist=distance;
  149.       };
  150.     };
  151.     to.x=[[theKliList objectAt:target] xLoc];
  152.     to.y=[[theKliList objectAt:target] yLoc];
  153.     
  154.     [[theShot setTo:&to] setFrom:&from];
  155.     [theWeaponList addObject:theShot];
  156.     
  157.     [[theKliList objectAt:target] takeHitForPoints:FED_DAM];
  158.   };
  159.   
  160.   /* Do the Klingons */
  161.   minDist=10000.0;
  162.   for(i=0;i<SHOTS_PER_FRAME;i++) {
  163.     theShot=[[Weapon alloc] init];
  164.     shooter=RANDINT(NUMSHIPS-1);
  165.     from.x=[[theKliList objectAt:shooter] xLoc];
  166.     from.y=[[theKliList objectAt:shooter] yLoc];
  167.     
  168.     target=0; minDist=10000.0; 
  169.     for(j=0;j<NUMSHIPS;j++) {
  170.       to.x=[[theFedList objectAt:j] xLoc];
  171.       to.y=[[theFedList objectAt:j] yLoc];
  172.       
  173.       distance=sqrt(pow(fabs((double)to.x-(double)from.x), 2)+pow(fabs((double)to.y-(double)from.y), 2));
  174.       if( distance <= minDist ){
  175.     target=j;
  176.     minDist=distance;
  177.       };
  178.     };
  179.     to.x=[[theFedList objectAt:target] xLoc];
  180.     to.y=[[theFedList objectAt:target] yLoc];
  181.     
  182.     [[theShot setTo:&to] setFrom:&from];
  183.     [theWeaponList addObject:theShot];
  184.     
  185.     [[theFedList objectAt:target] takeHitForPoints:KLI_DAM];
  186.     
  187.   };
  188.   return self;
  189. }
  190.  
  191. - oneStep
  192. /* This is called by BackSpace as fast as possible */
  193. {
  194.   [self shootPhaser];
  195.   [self drawPhaser];
  196.   [theFedList makeObjectsPerform:@selector(oneStep)];
  197.   [theKliList makeObjectsPerform:@selector(oneStep)];
  198.   [self erasePhaser];
  199.   
  200.   return self;
  201. }
  202.  
  203. - inspector:sender
  204. {
  205.   char buf[MAXPATHLEN];
  206.   
  207.   if (!sharedInspectorPanel)
  208.   {
  209.     sprintf(buf,"%s/Battle.nib",[sender moduleDirectory:"Battle"]);
  210.     [NXApp loadNibFile:buf owner:self withNames:NO];
  211.   }
  212.   return sharedInspectorPanel;
  213. }
  214.  
  215.  
  216. - sizeTo:(NXCoord)width :(NXCoord)height
  217. {
  218.   int i;
  219.   
  220.   [super sizeTo:width :height];
  221.   for(i=0;i<NUMSHIPS;i++) {
  222.     [[theFedList objectAt:i] setViewXSize:width YSize:height];
  223.     [[theKliList objectAt:i] setViewXSize:width YSize:height];
  224.   };
  225.   return self;
  226. }
  227.  
  228. @end
  229.